<div class="wrapper-bz-page">
    <div class="wrapper-bz-body">
        <div class="column-block block-left" style="width:200px;">
            <div class="column-title">页面</div>
            <div class="column-content">
                <div class="column-filter">

                </div>
                <div class="column-datagrid">
                    <table id="table_page_custom_in_column" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>
        <div class="column-block block-right" style="left:210px;">
            <div class="column-title">页面相关列</div>
            <div class="column-content">
                <div class="column-filter">

                </div>
                <div class="column-datagrid">
                    <table id="table_page_column" style="width:100%;height:100%"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="dialog_add_page_custom_in_column"></div>
<div id="dialog_edit_page_custom_in_column"></div>
<div id="dialog_add_page_column"></div>
<div id="dialog_edit_page_column"></div>
<script>
    $(function(){
        var subject_1 = "page_custom_in_column";
        var subject_2 = "page_column";
        //列表-对话框-表单
        var tableId_1 = '#table_' + subject_1;
        var tableId_2 = '#table_' + subject_2;

        var tableObject_1 = $(tableId_1);
        var tableObject_2 = $(tableId_2);
        var addDialogId_2 = '#dialog_add_' + subject_2;
        var editDialogId_2 = '#dialog_edit_' + subject_2;
        var addFormId_2 = '#form_add_' + subject_2;
        var editFormId_2 = '#form_edit_' + subject_2;

        var paramDataAdd_2 = {};
        var paramDataEdit_2 = {};

        var selectLeftMainId = 0;
        var configDialogAdd_2 = {
            href:'/admin/PageColumn/pageAdd',
            title:'新增页面纵列',
            queryParams:{},
            width:dialogTwoColumnWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataAdd_2 = $(addFormId_2).serializeObject();
                    paramDataAdd_2 = $.extend(paramDataAdd_2, {page_id:selectLeftMainId});
                    console.log(paramDataAdd_2);
                    if(!paramDataAdd_2.column_name){
                        layer.msg("请填写列标识码");
                        return ;
                    }
                    if(!paramDataAdd_2.column_title){
                        layer.msg("请填写列名");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/PageColumn/createPageColumn',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataAdd_2,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(addDialogId_2).dialog('close');
                                $(tableId_2).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(editDialogId).dialog('close');
                }
            }]
        };
        var configDialogEdit = {
            href:'role/edit',
            title:'修改角色',
            queryParams:{},
            width:dialogDefaultWidth,
            height:dialogDefaultHeight,
            modal:true,
            buttons:[{
                text:'<i class="fa fa-check fa-btn-cls" style="color:green"></i>保存',
                plain:true,
                handler:function(){
                    paramDataEdit = $(editFormId).serializeObject();
                    console.log(paramDataEdit);
                    if(!paramDataEdit.role_name){
                        layer.msg("请填写角色名称");
                        return ;
                    }
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: '/api/role/edit',
                        async: false,
                        dataType:'json',
                        type:'post',
                        data:paramDataEdit,
                        success:function(resp){
                            layer.close(loading);
                            if( 1 == resp.code ){
                                $(editDialogId).dialog('close');
                                $(tableId).datagrid('reload');
                            }else{
                                layer.msg(resp.message);
                            }
                        }
                    });
                }
            },{
                text:'<i class="fa fa-times fa-btn-cls" style="color:red"></i>取消',
                plain:true,
                handler:function(){
                    $(editDialogId).dialog('close');
                }
            }]
        };

        var configDatagrid_1 = {
            url:'/api/PageCustom/getPageCustomList',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[30,50,100],
            pageSize:30,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:false,
            singleSelect:true,
            checkOnSelect:true,
            selectOnCheck:true,
            rownumbers:false,
            striped:true,
            fitColumns:true,
            fit:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
            ]],
            columns:[[
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'page_name',title:'页面名称',width:100},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onSelect: function(index, row){
                console.log("enter select row");
                selectLeftMainId = row.id;
                configDatagrid_2.queryParams = {page_id:row.id};
                tableObject_2.datagrid(configDatagrid_2);
            },
            onClickRow: function(index, row){
                // console.log("enter click row");
                // selectLeftMainId = row.id;
                // configDatagrid_2.queryParams = {page_id:row.id};
                // tableObject_2.datagrid(configDatagrid_2);
            },
            onCheck: function(index, row){
            },
            onUncheck: function(index, row){
            },
            rowStyler: function(index, row){
                if(row.role_user_id > 0 && selectRoleId > 0){
                    return 'background:#c6e9ff';
                }
            },
            onLoadSuccess: function(){
                tableObject_1.datagrid('selectRow', 0);
                $(".datagrid-header-check").html("");
            },
            toolbar:[
                {
                    text:'<i class="fa fa-refresh tool-button-refresh"></i> 刷新',
                    plain:true,
                    handler:function(){
                        tableObject_1.datagrid('reload');
                    }
                }
            ]

        };
        var configDatagrid_2 = {
            url:'/api/PageColumn/getPageColumnList',
            method: 'post',
            remoteSort:false,
            queryParams:{},
            pageList:[30,50,100],
            pageSize:30,
            pagination:true,
            clickToEdit:false,
            dblclickToEdit:true,
            singleSelect:false,
            checkOnSelect:false,
            selectOnCheck:false,
            rownumbers:false,
            striped:true,
            frozenColumns:[[
                {field:'ck',title:'ck',checkbox:true},
                {field:'id',title:'ID',width:40,hidden:true},
                {field:'column_name',title:'列标识码',width:150},
                {field:'column_title',title:'列名',width:150, editor:'textbox'},
            ]],
            columns:[[
                {field:'column_width',title:'列宽',width:100, editor:'textbox'},
                {field:'element_type',title:'元素类型',width:100},
                {field:'fix_position',title:'标题位置',width:100},
                {field:'sort',title:'排序',width:100, editor:'textbox'},
                {field:'is_in_add',title:'新增是否包含',width:100, formatter: function (value, row, index) {
                        if (value == 1) {
                            return "<form><input data-field='is_in_add' data-id=" + row.id + "  checked  class=\"easyui-switchbutton switchBtn\" ></form>"
                        } else {
                            return "<form><input data-field='is_in_add' data-id=" + row.id + "  class=\"easyui-switchbutton switchBtn\"></form>"
                        }
                    }
                },
                {field:'is_in_edit',title:'编辑是否包含',width:100, formatter: function (value, row, index) {
                        if (value == 1) {
                            return "<form><input data-field='is_in_edit' data-id=" + row.id + "  checked  class=\"easyui-switchbutton switchBtn\" ></form>"
                        } else {
                            return "<form><input data-field='is_in_edit' data-id=" + row.id + "  class=\"easyui-switchbutton switchBtn\"></form>"
                        }
                    }},
                {field:'is_require',title:'是否必填',width:100, formatter: function (value, row, index) {
                        if (value == 1) {
                            return "<form><input data-field='is_require' data-id=" + row.id + "  checked  class=\"easyui-switchbutton switchBtn\" ></form>"
                        } else {
                            return "<form><input data-field='is_require' data-id=" + row.id + "  class=\"easyui-switchbutton switchBtn\"></form>"
                        }
                    }},
                {field:'is_sort',title:'是否支持排序',width:100, formatter: function (value, row, index) {
                        if (value == 1) {
                            return "<form><input data-field='is_sort' data-id=" + row.id + "  checked  class=\"easyui-switchbutton switchBtn\" ></form>"
                        } else {
                            return "<form><input data-field='is_sort' data-id=" + row.id + "  class=\"easyui-switchbutton switchBtn\"></form>"
                        }
                    }},
                {field:'is_hide',title:'是否隐藏',width:100, formatter: function (value, row, index) {
                        if (value == 1) {
                            return "<form><input data-field='is_hide' data-id=" + row.id + "  checked  class=\"easyui-switchbutton switchBtn\" ></form>"
                        } else {
                            return "<form><input data-field='is_hide' data-id=" + row.id + "  class=\"easyui-switchbutton switchBtn\"></form>"
                        }
                    }},
                {field:'status',title:'状态',width:50, align: 'center', formatter: function (value, row, index) {
                        if (1 == value) {
                            return '<span class="cell cell-yes"></span>';
                        } else if (2 == value) {
                            return '<span class="cell cell-no"></span>';
                        }
                    }},
                {field:'create_time',title:'创建时间',width:100},
                {field:'update_time',title:'更新时间',width:100},
            ]],
            loadFilter: function(data){
                return loadFilter(data, true);
            },
            onCheck: function(row){
            },
            onUncheck: function(row){
            },
            rowStyler: function(row){
                if(row.role_menu_id > 0 && selectRoleId > 0){
                    return 'background:#c6e9ff';
                }
            },
            onEndEdit: function(index, row, changes) { //在完成编辑但编辑器还没有销毁之前触发
                console.log("end edit:", index, row);
                if (JSON.stringify(changes) != '{}') {
                    var updateData = $.extend({id:row.id}, changes);
                    $.ajax({
                        url:'/api/PageColumn/simpleUpdatePageColumn',
                        type:'post',
                        data: updateData,
                        dataType:'json',
                        async:false,
                        success:function(resp){
                            if(0 == resp.code){
                                layer.msg(resp.message);
                            }else{
                                //tableObject_2.datagrid('updateRow', index);
                                tableObject_2.datagrid('reload');
                            }
                        }
                    });
                }
            },
            onLoadSuccess:function(){
                $(".switchBtn").switchbutton({
                    height: 18,
                    onText: "是",
                    offText: "否",
                    onChange: function(value){
                        var newValue = value ? 1:0;
                        var pageColumnId = $(this).data("id");
                        var fieldName = $(this).data("field");
                        togglePageColumnInfo(pageColumnId, fieldName, newValue);
                    }
                })
            },
            toolbar:[
                {
                    text:'<i class="fa fa-refresh" style="color:limegreen"></i> 刷新',
                    plain:true,
                    handler:function(){
                        tableObject_2.treegrid('reload');
                    }
                },
                {
                    text:'<i class="fa fa-refresh tool-button-add"></i> 新增',
                    plain:true,
                    handler:function(){
                        $(addDialogId_2).dialog(configDialogAdd_2);
                    }
                },
                {
                    text:'<i class="fa fa-refresh tool-button-edit"></i> 编辑',
                    plain:true,
                    handler:function(){
                        tableObject_1.datagrid('reload');
                    }
                },
                {
                    text:'<i class="fa fa-refresh tool-button-delete"></i> 删除',
                    plain:true,
                    handler:function(){
                        tableObject_1.datagrid('reload');
                    }
                }
            ]

        };
        tableObject_1.datagrid(configDatagrid_1);

        // SearchCombobox('search_input_user_in_role','table_user_in_role',function (val,name) {
        //     configDatagrid_1.queryParams = {};
        //     configDatagrid_1.queryParams[name] = val;
        //     if(selectRoleId){
        //         configDatagrid_1.queryParams['role_id'] = selectRoleId;
        //     }
        //     tableObject_1.datagrid(configDatagrid_1);
        // },['ck','ID']);
        tableObject_2.datagrid(configDatagrid_2).datagrid("enableCellEditing");

        function getCheckedRowData(opText){
            var checkList = tableObject.datagrid('getChecked');
            if(checkList.length < 1){
                layer.msg('请选择要'+ opText +'的数据');
                return false;
            }
            return checkList;
        }

        function getCheckedRowData_1(opText){
            var checkList = tableObject_1.datagrid('getChecked');
            if(checkList.length < 1){
                layer.msg('请选择要'+ opText +'的数据');
                return false;
            }
            return checkList;
        }

        function getCheckedRowData_2(opText){
            var checkList = tableObject_2.datagrid('getChecked');
            if(checkList.length < 1){
                layer.msg('请选择要'+ opText +'的数据');
                return false;
            }
            return checkList;
        }

        function togglePageColumnInfo(pageColumnId, fieldName, value)
        {
            console.log("update page column", pageColumnId);
            $.ajax({
                url:'/api/PageColumn/togglePageColumn',
                type:'post',
                data: {id: pageColumnId, field_name: fieldName, value:value},
                dataType:'json',
                async:false,
                success:function(resp){
                    if(0 == resp.code){
                        layer.msg(resp.message);
                    }
                }
            });
        }

    });


</script>
